import React,{Component} from 'react';
import fetch from '../../../utils/request';
import newService from '../../../utils/authToken'
import { Link, withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import { Button,Form,Input } from 'antd';




@withRouter
class ConfigLogin extends Component{
    state={
    }
    formRef=React.createRef();
    submit=()=>{  
            this.formRef.current.validateFields().then((values) => {
            let username=values.username;
            let password=values.password;
            let params={
                 username,
                 password
             }
             //登录首次获取token,localStorage存储，store首次存储token
             newService()({ url: "/api/login", method: "post", params}).then((res)=>{
                 console.log(res);
                 const userLevel=res.data.data.userLevel; 
                 localStorage.setItem('userLevel',userLevel);
                 const userData=res.data.data;
                 this.props.saveUserData(userData);
             })
        });
    }
    render(){
          const formItemStyle ={
            color: 'rgb(0, 126, 220)',
            fontWeight: 'bold'
        };
        const formStyle={
            margin:'auto'
        }
        return(
            <Form
            style={formStyle}
            labelCol={{ span: 24 }}
            wrapperCol={{ span:24}}
            ref={this.formRef}
            onFinish={this.submit}

        >
            <Form.Item
                label={<span style={formItemStyle}>登录名</span>}
                name="username"
            >
                <Input />
            </Form.Item>
            <Form.Item
                label={<span style={formItemStyle}>密码</span>}
                name="password"
            >
                <Input />
            </Form.Item>
            <Button htmlType="submit">登录</Button>
        </Form>
        )
    }
}

const mapStateToProps = (state) => {
    return {
        userLevel:state.getIn(['organization','userLevel'])
    }
}

const mapDispatchToProps = (dispatch) => ({
    setCurrentToken(decode){
        dispatch({
            type:'set_current_token',
            currentToken:decode
        })
    },
    err(data){
         dispatch({
             type:'err_token',
         currentToken:data
         })
    },
    saveUserData(userData){
        dispatch({
            type:'save_user_data',
            userData:userData,
        })
    }
})

export default connect(mapStateToProps, mapDispatchToProps)(ConfigLogin);
